package com.easyadmin.pro.modules.sys.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.easyadmin.pro.common.constants.StatusConstants;
import com.easyadmin.pro.common.constants.SystemConstants;
import com.easyadmin.pro.common.vo.RouterTree;
import com.easyadmin.pro.modules.sys.entity.RouterEntity;
import com.easyadmin.pro.modules.sys.mapper.RouterMapper;
import com.easyadmin.pro.modules.sys.query.RouterQueryVo;
import com.easyadmin.pro.modules.sys.rest.RouterInfoResult;
import com.easyadmin.pro.modules.sys.service.IRouterService;
import com.easyadmin.pro.modules.sys.vo.RouterInfoVo;
import com.easyadmin.pro.tool.RedisUtils;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 *  服务层实现。
 *
 * @author xulin
 * @since 2024-04-02
 */
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class RouterServiceImpl extends ServiceImpl<RouterMapper, RouterEntity> implements IRouterService {

    private final RouterMapper routerMapper;

    @Override
    public List<RouterInfoResult> queryRouterPage(RouterQueryVo routerQuery) {
        return routerMapper.queryRouterList(routerQuery);
    }

    @Override
    public List<RouterTree> initRouterTree(Long parentId) {
        List<RouterTree> routerTrees = new ArrayList<>();
        List<RouterEntity> routerInfos = routerMapper.selectListByQuery(new QueryWrapper()
                .eq(RouterEntity::getStatus, StatusConstants.STATUS_1));
        listToTree(routerTrees, routerInfos, parentId);
        return routerTrees;
    }

    @Override
    public void refresh(String refreshKey) {
        RedisUtils.removeData(refreshKey);
        List<RouterTree> routerTrees = initRouterTree(0L);
        RedisUtils.setValue(refreshKey, routerTrees);
    }

    @Override
    public Integer disabledRouter(RouterInfoVo routerInfo) {
        RouterEntity routerEntity = routerMapper.selectOneById(routerInfo.getId());
        routerEntity.setStatus(StatusConstants.STATUS_0);
        return routerMapper.update(routerEntity);
    }

    @Override
    public List<RouterTree> cacheRouter() {
        return JSONObject.parseArray((String) RedisUtils.getValue(SystemConstants.ROUTER_KEY), RouterTree.class);
    }

    /**
     * 转换树
     * @param routerTrees
     * @param routerInfos
     * @param parentId
     */
    private void listToTree(List<RouterTree> routerTrees,
                            List<RouterEntity> routerInfos,
                            Long parentId) {
        routerInfos.forEach(routerInfo -> {
            if (Objects.equals(parentId, routerInfo.getParentId())) {
                RouterTree routerTree = new RouterTree(routerInfo.getId(), routerInfo.getTitle(), new ArrayList<>());
                listToTree(routerTree.getChildren(), routerInfos, routerInfo.getId());
                routerTrees.add(routerTree);
            }

        });
    }

}
